package net.xiezihao.fuxiclient.demo.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import net.xiezihao.fuxiclient.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/demoClient")
@EnableCircuitBreaker
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/get")
    public Object getProduct(@RequestParam (value = "id") int id){
        Object project = productService.getProject(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info",project);
        return map;
    }

    @RequestMapping("/getFeign")
    @HystrixCommand(fallbackMethod = "defaultStores")
    public Object getProductFeign(@RequestParam (value = "id") int id){
        Object project = productService.getProjectByFeign(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info",project);
        return map;
    }

    public Object defaultStores(int id) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",-1);
        map.put("info","断路器启用");
        return map;
    }
}
